﻿/* Copyright (C) 2011 by John Cronin
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:

 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

using System;
using System.Collections.Generic;
using System.Text;

namespace tysila
{
    class x86_64_CallConv : CallConv
    {
        public static CallConv isr(Assembler.MethodToCompile mtc, StackPOV pov, Assembler ass, ThreeAddressCode call_tac)
        {
            CallConv ret = new CallConv();

            ret.CallerCleansStack = true;
            ret.CallTac = ThreeAddressCode.Op.call_void;

            /* For an isr on the x86_64, the stack is either:
             * 
             * if no error code:
             * [rbp + 0]:       previous rbp
             * [rbp + 8]:       return address
             * [rbp + 16]:      return cs
             * [rbp + 24]:      saved rflags
             * 
             * if an error code:
             * [rbp + 0]:       previous rbp
             * [rbp + 8]:       error code
             * [rbp + 16]:      return address
             * [rbp + 24]:      return cs
             * [rbp + 32]:      saved rflags
             */

            Signature.Method m = mtc.msig.Method;

            if (m.Params.Count == 0)
                ret.Arguments = new List<ArgumentLocation>();
            else if (m.Params.Count == 1)
                ret.Arguments = new List<ArgumentLocation> { new ArgumentLocation { ValueLocation = new hardware_contentsof { base_loc = x86_64_Assembler.Rbp, const_offset = 8, size = 8 }, ValueSize = 8 } };
            else
                throw new Exception("No more than one argument is allowed on an ISR");

            ret.StackSpaceUsed = 0;
            ret.ReturnValue = null;

            return ret;
        }

        public static CallConv tcdecl(Assembler.MethodToCompile mtc, StackPOV pov, Assembler ass, ThreeAddressCode call_tac)
        {
            CallConv ret = new CallConv();

            ret.CallerCleansStack = true;
            ret.CallTac = call_tac.Operator;

            /* On the x86_64 stack, arguments are 8 byte aligned
             * 
             * From the function's point of view (pov == Callee), layout is:
             * 
             * [rbp + 0]:       previous ebp
             * [rbp + 8]:       return address
             * [rbp + 16]:      argument 0
             * etc
             * 
             * Value types are passed by value on the stack
             */

            ret.Arguments = new List<ArgumentLocation>();

            int stack_pos = 0;
            int pointer_size = 8;
            hardware_location base_reg = x86_64_Assembler.Rsp;
            if (pov == CallConv.StackPOV.Callee)
            {
                stack_pos = 16;
                base_reg = x86_64_Assembler.Rbp;
            }

            Signature.Method m = null;
            if (mtc.msig is Signature.Method)
                m = mtc.msig as Signature.Method;
            else if (mtc.msig is Signature.GenericMethod)
                m = ((Signature.GenericMethod)mtc.msig).GenMethod;
            else
                throw new NotSupportedException();

            if (m.HasThis && !m.ExplicitThis)
            {
                ret.Arguments.Add(new ArgumentLocation { ValueLocation = new hardware_contentsof { base_loc = base_reg, const_offset = stack_pos, size = pointer_size }, ValueSize = pointer_size });
                stack_pos += pointer_size;
            }

            List<ArgumentLocation> vtype_locs = new List<ArgumentLocation>();
            foreach (Signature.Param p in m.Params)
            {
                int v_size = ass.GetSizeOf(p);
                if (v_size < 8)
                    v_size = 8;
                ret.Arguments.Add(new ArgumentLocation { ValueSize = v_size, ValueLocation = new hardware_contentsof { base_loc = base_reg, const_offset = stack_pos, size = v_size } });

                /*
                if (v_size > pointer_size)
                {
                    ArgumentLocation vtype_loc = new ArgumentLocation { ReferenceLocation = new hardware_contentsof { base_loc = base_reg, const_offset = stack_pos, size = pointer_size }, ValueSize = v_size };
                    ret.Arguments.Add(vtype_loc);
                    vtype_locs.Add(vtype_loc);
                    v_size = pointer_size;
                }
                else
                    ret.Arguments.Add(new ArgumentLocation { ValueSize = v_size, ValueLocation = new hardware_contentsof { base_loc = base_reg, const_offset = stack_pos, size = pointer_size } }); */

                stack_pos += v_size;
                stack_pos = util.align(stack_pos, pointer_size);
            }

            /*foreach (ArgumentLocation vtype_loc in vtype_locs)
            {
                vtype_loc.ValueLocation = new hardware_contentsof { base_loc = base_reg, const_offset = stack_pos, size = vtype_loc.ValueSize };
                stack_pos += vtype_loc.ValueSize;
                stack_pos = util.align(stack_pos, pointer_size);
            }*/

            if (call_tac.Operator == ThreeAddressCode.Op.call_void)
                ret.ReturnValue = null;
            else
            {
                var_semantic ret_vs = call_tac.GetResultSemantic(ass);

                if (ret_vs.needs_integer)
                    ret.ReturnValue = x86_64_Assembler.Rax;
                else if (ret_vs.needs_float)
                    ret.ReturnValue = x86_64_Assembler.Xmm0;
                else
                {
                    int r_size = ass.GetSizeOf(m.RetType);
                    ret.ReturnValue = new hardware_contentsof { base_loc = base_reg, const_offset = stack_pos, size = r_size };
                    stack_pos += r_size;
                    stack_pos = util.align(stack_pos, pointer_size);
                }
            }

            ret.StackSpaceUsed = stack_pos;

            return ret;
        }
    }

    partial class x86_64_Assembler
    {
        protected override void arch_init_callconvs()
        {
            call_convs.Clear();
            call_convs.Add("cdecl", x86_64_CallConv.tcdecl);
            call_convs.Add("tcdecl", x86_64_CallConv.tcdecl);
            call_convs.Add("default", x86_64_CallConv.tcdecl);
            call_convs.Add("isr", x86_64_CallConv.isr);
        }

        protected CallConv callconv_conv_u4_r8
        {
            get
            {
                return MakeStaticCall("default", new Signature.Param(BaseType_Type.R8), new List<Signature.Param> { new Signature.Param(BaseType_Type.U4) }, ThreeAddressCode.Op.call_r8);
            }
        }
        protected CallConv callconv_conv_u8_r8
        {
            get
            {
                return MakeStaticCall("default", new Signature.Param(BaseType_Type.R8), new List<Signature.Param> { new Signature.Param(BaseType_Type.U8) }, ThreeAddressCode.Op.call_r8);
            }
        }
    }
}
